/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.wechat.service;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.BaseService;
import com.boxin.ims.modules.wechat.entity.Seckill;
import com.boxin.ims.modules.wechat.score.entity.ScoreExchange;
import com.boxin.ims.modules.wechat.dao.SeckillDao;

/**
 * 微信秒杀Service
 * @author Jakemanse
 * @version 2013-09-13
 */
@Component
@Transactional(readOnly = true)
public class SeckillService extends BaseService {

	@SuppressWarnings("unused")
	private static Logger logger = LoggerFactory.getLogger(SeckillService.class);
	
	@Autowired
	private SeckillDao seckillDao;
	
	public Seckill get(Long id) {
		return seckillDao.findOne(id);
	}
	
	public Page<Seckill> find(Page<Seckill> page, Seckill seckill) {
		DetachedCriteria dc = seckillDao.createDetachedCriteria();
		dc.createAlias("user", "user");
		if (seckill.getUser()!=null &&  seckill.getUser().getId()!=null&&!seckill.getUser().isAdmin()){
			dc.add(Restrictions.eq("user.id", seckill.getUser().getId()));
		}
		if (seckill.getUser()!=null &&StringUtils.isNotEmpty(seckill.getUser().getName())){
			dc.add(Restrictions.like("user.name", "%"+seckill.getUser().getName()+"%"));
		}
		if (StringUtils.isNotEmpty(seckill.getName())){
			dc.add(Restrictions.like("name", "%"+seckill.getName()+"%"));
		}
		if (StringUtils.isNotEmpty(seckill.getRemarks())){
			dc.add(Restrictions.like("remarks", "%"+seckill.getRemarks()+"%"));
		}
		if ("1".equals(seckill.getType())){
			dc.add(Restrictions.eq("type", seckill.getType()));
		}else{
			dc.add(Restrictions.eq("type", "1"));
		}
		dc.add(Restrictions.eq("delFlag", Seckill.DEL_FLAG_NORMAL));
		dc.addOrder(Order.desc("id"));
		return seckillDao.find(page, dc);
	}
	
	public Long getCount(Long userId ){
		DetachedCriteria dc = seckillDao.createDetachedCriteria();
		dc.add(Restrictions.eq("user.id", userId));
		dc.add(Restrictions.eq("type", "1"));
		return seckillDao.count(dc);
		
	}
	
	public Page<Seckill> findByMember(Page<Seckill> page, Seckill seckill) {
		DetachedCriteria dc = seckillDao.createDetachedCriteria();
		dc.createAlias("user", "user");
		if (seckill.getUser()!=null &&  seckill.getUser().getId()!=null&&!seckill.getUser().isAdmin()){
			dc.add(Restrictions.eq("user.id", seckill.getUser().getId()));
		}
		if (seckill.getUser()!=null &&StringUtils.isNotEmpty(seckill.getUser().getName())){
			dc.add(Restrictions.like("user.name", "%"+seckill.getUser().getName()+"%"));
		}
		if (StringUtils.isNotEmpty(seckill.getName())){
			dc.add(Restrictions.like("name", "%"+seckill.getName()+"%"));
		}
		if (StringUtils.isNotEmpty(seckill.getRemarks())){
			dc.add(Restrictions.like("remarks", "%"+seckill.getRemarks()+"%"));
		}
		if ("1".equals(seckill.getType())){
			dc.add(Restrictions.eq("type", seckill.getType()));
		}
		dc.add(Restrictions.eq("delFlag", Seckill.DEL_FLAG_NORMAL));
		dc.addOrder(Order.desc("id"));
		return seckillDao.find(page, dc);
	}
	
	
	public List<Seckill> findAllByUserId(Long userId ){
		DetachedCriteria dc = seckillDao.createDetachedCriteria();
		if(userId==52){
			dc.add(Restrictions.eq("type", "1"));
		}else{
			dc.add(Restrictions.eq("user.id", userId));
		}
		dc.add(Restrictions.eq("delFlag", ScoreExchange.DEL_FLAG_NORMAL));
		dc.addOrder(Order.desc("startTime"));
		return seckillDao.find( dc);
		
	}
	
	
	public List<Seckill> findUserList( ){
		return seckillDao.find("from Seckill where type=1 group by user.id  order by sort  desc ", null);
		
	}
	
	
	public void updateSort(String sort ,Long userId ){
		seckillDao.update("update Seckill set sort="+sort+" where user.id="+userId,	null);
		
	}
	
	
	@Transactional(readOnly = false)
	public void save(Seckill seckill) {
		seckillDao.save(seckill);
	}
	
	@Transactional(readOnly = false)
	public void delete(Long id) {
		seckillDao.deleteById(id);
	}
	
	
	public void clear(){
		seckillDao.clear();
	}
	
}
